namespace CSharpPro._1013;
//
public class User
{
    private string _account = null;
    private string _password = null;
    public void Register()
    {
        Boolean flag = false;

        while (!flag)
        {
            Console.WriteLine("请输入你的用户名:");
            string input = Console.ReadLine();
            int length = input.Length;
            if (length < 3 || length > 15)
            {
                Console.WriteLine("用户名的长度必须为 3-15 个字符");
            }

            else
            { 
                bool hasInvalidChar = false; 
                foreach (char c in input)
                {
                    if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_'))
                    {
                        hasInvalidChar = true;
                        Console.WriteLine("用户名只能包含字母、数字、下划线（_）");
                        break; 
                    }
                }
                
                if (!hasInvalidChar)
                {
                    flag = true;
                    _account = input;
                    Console.WriteLine("用户名格式正确！");
                }
            }
        }
        
        flag = false;
        
        while (!flag)
        {
            int x = 0;
            int y = 0;
            int z = 0;
            
            Console.WriteLine("请输入你的密码:");
            string input = Console.ReadLine();
            int length = input.Length;
            if (length < 6 || length > 20)
            {
                Console.WriteLine("密码的长度必须为 6-20 个字符");
            }

            foreach (char c in input)
            {
                if (c >= 'A' && c <= 'Z')
                {
                    x++;
                }
                else if (c >= 'a' && c <= 'z')
                {
                    y++;
                }
                else if (c >= '0' && c <= '9')
                {
                    z++;
                }
            }

            if (x > 0 && y > 0 && z > 0)
            {
                flag = true;
                _password = input;
                while (true)
                {
                    Console.WriteLine("请再次确认你的密码:");
                    string input2 = Console.ReadLine();
                    if (input2 == input)
                    {
                        break;
                    }
                    Console.WriteLine("密码与确认密码必须一致");
                }
            }
            else
            {
                Console.WriteLine("密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字");
            }
        }
        
        Console.WriteLine("注册成功");
    }

    public void Login()
    {
        if (_account == null)
        {
            Console.WriteLine("未检测到注册账号，请先注册！");
            Register();
        }
        else
        {
            Boolean flag = false;
            while (!flag)
            {
                Console.WriteLine("请输入你的用户名:");
                string inputAccount = Console.ReadLine();
                if (inputAccount == _account)
                {
                    Console.WriteLine("请输入你的密码:");
                    string inputPassword =  Console.ReadLine();
                    if (inputPassword == _password)
                    {
                        flag = true;
                        Console.WriteLine("登录成功");
                    }
                    else
                    {
                        Console.WriteLine("密码错误");
                    }
                }
                else
                {
                    Console.WriteLine("用户名不存在");
                }
            }
        }
        
    }
    
}